// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;
import RoadNode from "../map/road/RoadNode";
import MapRoadUtils from "../map/road/MapRoadUtils";
import { RoadState } from "../map/base/MapType";
@ccclass
export default class NewClass extends cc.Component {
    // LIFE-CYCLE CALLBACKS:
    @property(cc.Label)
    pPosLabel:cc.Label = null;
    private _shopColor = [cc.color(255,0,0,255),cc.color(255,0,163,255),cc.color(153,0,255,255),cc.color(31,0,255,255),cc.color(194,255,255,255),cc.color(0,255,0,255),cc.color(255,255,0,255),cc.color(255,102,0,255),cc.color(0,0,0,255),cc.color(255,255,255,255)]
    // onLoad () {}
    private _roadDic:{[key:string]:RoadNode} = {};
    start () {
    }

    //刷新路径上的点的属性
    updateRoadDic(roadDic:{[key:string]:RoadNode}){
        this._roadDic = roadDic;
        this.updateHoneyComb();
    }

    //
    updateHoneyComb(){
        let graphics = this.getComponent(cc.Graphics);
        graphics.clear();
        for(let i in this._roadDic){
            this.drawHoneyComb(this._roadDic[i].px,this._roadDic[i].py,this._roadDic[i].value);
        }
    }

    //根据一个点计算点周围的六个点得出六边形的数组
    drawHoneyComb(px:number,py:number,value:number){
        let graphics = this.getComponent(cc.Graphics);
        if(value != RoadState.unreache){
            let pArray = this.getDrawNodeArray(px,py,MapRoadUtils.instance.halfNodeWidth-1);
            graphics.moveTo(pArray[0].x,pArray[0].y);
            graphics.lineTo(pArray[1].x,pArray[1].y);
            graphics.lineTo(pArray[2].x,pArray[2].y);
            graphics.lineTo(pArray[3].x,pArray[3].y);
            graphics.lineTo(pArray[4].x,pArray[4].y);
            graphics.lineTo(pArray[5].x,pArray[5].y);
            if(value == RoadState.walkNormal)
                graphics.fillColor = cc.color(0,255,0,100);//cc.Color.GREEN;
            else if(value == RoadState.optWalk){
                graphics.fillColor = cc.color(0,0,255,100);//cc.Color.GREEN;
            }else if(value >= RoadState.shop && value < RoadState.start){
                graphics.fillColor = this._shopColor[value-100];//cc.Color.GREEN;
            }else if(value == RoadState.start){
                graphics.fillColor = cc.color(0,255,255,200);//cc.Color.GREEN;
            }else if(value == RoadState.end){
                graphics.fillColor = cc.color(255,0,0,200);//cc.Color.GREEN;
            }else if(value == RoadState.event){
                graphics.fillColor = cc.color(0,0,0,255);
            }
            graphics.fill();
        }else{
            let pArray = this.getDrawNodeArray(px,py,MapRoadUtils.instance.halfNodeWidth);
            graphics.moveTo(pArray[0].x,pArray[0].y);
            graphics.lineTo(pArray[1].x,pArray[1].y);
            graphics.lineTo(pArray[2].x,pArray[2].y);
            graphics.lineTo(pArray[3].x,pArray[3].y);
            graphics.lineTo(pArray[4].x,pArray[4].y);
            graphics.lineTo(pArray[5].x,pArray[5].y);
            graphics.stroke();
        }
    }

    //根据当前位置刷新坐标显示
    updatePosInfo(pos:cc.Vec2){
        var roadNode = MapRoadUtils.instance.getNodeByPixel(pos.x,pos.y);
        this.pPosLabel.string = roadNode.toString();
    }


    //根据传入的点计算当前点的绘制六边形的顶点数组
	getDrawNodeArray(x:number,y:number,radius:number):cc.Vec2[]{
        let pArray = []; 
        for(let i = 0;i< 6;i++){
			let angle = i*60;
			let px = x + Math.cos(angle*Math.PI/180)* radius;
			let py = y + Math.sin(angle*Math.PI/180)* radius;
			pArray.push(cc.v2(px,py));
		}
		return pArray;
    }
    

    onFileOpenClick(){
        document.getElementById("file").click()
    }

    onSaveClick(){
        var sceneMap = cc.find("Canvas/SceneMap").getComponent("SceneMap");
        var dataStr = JSON.stringify(sceneMap._mapData);
        var element = document.createElement('a');
        element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(dataStr));
        element.setAttribute('download', sceneMap._mapData.name+".json");
        element.style.display = 'none';
        document.body.appendChild(element);
        element.click();
        document.body.removeChild(element);
    }
    // update (dt) {}
}
